package com.zjf.carpark.controller;


import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.utils.TencentCosUtil_py;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

import static com.zjf.carpark.utils.CodeAndMessage.*;


@Controller
@RequestMapping("/upload")
public class UpLoad {
    ResponseData responseData = new ResponseData(CODE0, ERROR);

    @RequestMapping("/show")
    @ResponseBody
    private ResponseData postUserInforUpDate(HttpServletRequest request, @RequestParam(required = false) MultipartFile file) throws IOException {

        // 文件是否为空
        if (!file.isEmpty()) {
            FileInputStream inputStream = (FileInputStream) file.getInputStream();
            // 图片链接
            String path = String.valueOf(UUID.randomUUID()); // KeyUtil.genUniqueKey()生成图片的随机名
            String filePath = TencentCosUtil_py.SimpleUploadFileFromStream(path, inputStream);
            System.out.println(filePath);
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("filePath", filePath);
        }
        return responseData;
    }
}
